package org.virtualnorthstar.benkyou.main;

import com.jme3.network.Client;
import com.jme3.network.Network;
import com.jme3.network.serializing.Serializer;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

public class Main {
    private InetAddress inetAddress;
    private String ipAddress;
    private Pattern ipPattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
  // private String gameServerIp = "199.17.224.227";
    private String gameServerIp = "192.168.0.1";
    private Client myClient;
    public void init(){
        try {
            inetAddress =InetAddress.getLocalHost();
            ipAddress = inetAddress.getHostAddress();
            
            do{
            ipAddress = JOptionPane.showInputDialog("Unable to auto load ip adress"+
                    "\n please enter ip adress in xxx.xxx.xxx.xxx format"+
                    "\nE.g 65.224.29.123");
            }while((ipAddress.equals("127.0.0.1") || ipAddress.equals("127.0.1.1")) || !ipPattern.matcher(ipAddress).matches());
        } catch (UnknownHostException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Serializer.registerClass(VNCServerLocationMessage.class);
            myClient = Network.connectToServer("127.0.0.1", 6001);
           myClient.start();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("connecting to benkyou server" + 
                "setting vnc server ip address to:" + ipAddress);
         myClient.send(new VNCServerLocationMessage(ipAddress));
        JOptionPane.showMessageDialog(null, "VNC Server connected to benkyou gaming server"
                +"\n press ok to disconnect");
        myClient.send(new VNCServerLocationMessage("null"));
        System.exit(0);
    } 
    public static void main(String args[]){
        Main m = new Main();
        m.init();
    }
}